<?php

use App\Models\Commodity;
use App\Models\Classes;
use App\Models\ChildClass;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});


Route::middleware([])->group(function (){
    Route::get('/items','\App\Http\Controllers\Api\CommodityController@entry');

    // 一级分类
    Route::get('/class',function (){

        return tap(Classes::orderBy('pa_sort','desc')->paginate(
                Request::capture()->get('num',5)
            ),function ($values){
            foreach ($values->items() as $key=>$item){
                $item->childClass;
            }
        });
    });

    // 二级分类
    Route::get('/subClass',function (){
        return tap(ChildClass::where(
            'pa_parent','=',
            Request::capture()->get('pid',1)
        )->paginate(
            Request::capture()->get('num',5)
        ), function ($values){

        });
    });

    // 商品详细
    Route::get('/detail',function (){
        return Commodity::find(
            Request::capture()->get('id',1)
        );
    });

    Route::get('/search',function (){
        return tap(
            Commodity::search(Request::capture()->get('keywords',''))
                ->paginate(Request::capture()
                    ->get('limit',5)),
            function ($value){
                foreach ($value->items() as $key=>$item){
                    //$item->childClass->pa_name
                    $item->childClass->parentClass;
                }
            });
    });
});